package com.shaoshaohuo.app.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.NewPickEntity;
import com.shaoshaohuo.app.ui.DetailsActivity;
import com.shaoshaohuo.app.ui.SureOrderPayInformation;
import com.shaoshaohuo.app.utils.CircleImageView;
import com.shaoshaohuo.app.utils.FiveCornerStar;
import com.shaoshaohuo.app.view.NumberView;
import com.shaoshaohuo.app.view.WhView;
import com.squareup.picasso.Picasso;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品詳情頁上半部分
 * Created by Administrator on 2017/6/9.
 */

public class PickDetailsFragmentTop extends Fragment implements View.OnClickListener, WhView.OnWhlistener {

    private NewPickEntity top;
    private ArrayList<View> views;
    private TextView fragment_detatop_iv_sc;
    private NewPickEntity.DataBean data;
    private NumberView deatils_numberView;
    private Button fragment_detalis_top_btn_gm;
    private WhView pick_whyuanjia;
    private WhView pick_whchild;
    private WhView pick_whtem;
    private Button pick_zongjia;
    private Float aFloat;
    private Float aFloat1;
    private Float aFloat2;
    private int getnum;
    private int getnum1;
    private int getnum2;

    public static PickDetailsFragmentTop newInstance(Serializable serializable) {
        Bundle args = new Bundle();
        args.putSerializable("top", serializable);
        PickDetailsFragmentTop fragment = new PickDetailsFragmentTop();
        fragment.setArguments(args);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.pick_fragment_details_top, null);
        return inflate;

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle arguments = getArguments();

        top = (NewPickEntity) arguments.getSerializable("top");
        init(view);
        initListener();
    }

    private void initListener() {
        fragment_detalis_top_btn_gm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), SureOrderPayInformation.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("newPickEntity", top);
                bundle.putInt("yuanjia", getnum);
                bundle.putInt("child", getnum1);
                bundle.putInt("gongxiang", getnum2);
                bundle.putString("type", "2");
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    private void init(View view) {
        FiveCornerStar pick_fivestart = (FiveCornerStar) view.findViewById(R.id.pick_fivestart);
        pick_fivestart.setNum(5);
        pick_fivestart.setIsdown(false);
        ImageView fragment_detatop_vp = (ImageView) view.findViewById(R.id.fragment_detatop_vp);
        fragment_detatop_vp.getLayoutParams().height = getResources().getDisplayMetrics().heightPixels * 2 / 3;
        String img = top.getData().getImg();
        Picasso.with(getContext()).load(img).placeholder(R.mipmap.logo).into(fragment_detatop_vp);
        TextView fragment_detatop_tv_nmae = (TextView) view.findViewById(R.id.fragment_detatop_tv_nmae);
        fragment_detatop_tv_nmae.setText(top.getData().getName());
        TextView piuck_wifi = (TextView) view.findViewById(R.id.piuck_wifi);
        if (top.getData().getWifi().equals("1")) {

            piuck_wifi.setText("提供WiFi");
        } else {
            piuck_wifi.setText("没有WiFi");
        }
        TextView fragment_detatop_tv_detalis = (TextView) view.findViewById(R.id.fragment_detatop_tv_detalis);
        fragment_detatop_tv_detalis.setText(top.getData().getAddress());
        TextView pick_tv_yuanjia = (TextView) view.findViewById(R.id.pick_tv_yuanjia);
        //¥60.00/人
        pick_tv_yuanjia.setText("¥" + top.getData().getAdult() + "/人");
        TextView pick_tv_gongxiangjia = (TextView) view.findViewById(R.id.pick_tv_gongxiangjia);
        pick_tv_gongxiangjia.setText("¥" + top.getData().getTeam() + "/人");
        TextView pick_tv_ertongjia = (TextView) view.findViewById(R.id.pick_tv_ertongjia);
        pick_tv_ertongjia.setText("¥" + top.getData().getChild() + "/人");
        TextView pick_xianliang = (TextView) view.findViewById(R.id.pick_xianliang);
        pick_xianliang.setText("限量" + top.getData().getShareMax() + "人");
        pick_zongjia = (Button) view.findViewById(R.id.pick_zongjia);
        pick_whyuanjia = (WhView) view.findViewById(R.id.pick_whyuanjia);
        pick_whyuanjia.setWhlistener(this);
        pick_whchild = (WhView) view.findViewById(R.id.pick_whchild);
        pick_whchild.setWhlistener(this);
        pick_whtem = (WhView) view.findViewById(R.id.pick_whtem);
        fragment_detalis_top_btn_gm = (Button) view.findViewById(R.id.fragment_detalis_top_btn_gm);

        pick_whtem.setWhlistener(this);
        overmoney();
        /**
         * 评论相关
         */
        List<NewPickEntity.DataBean.RemarkBean> a = data.getRemark();
        TextView pj_user_value = (TextView) view.findViewById(R.id.pj_user_value);
        if (a.size() > 0) {
            FiveCornerStar pj_user_xx = (FiveCornerStar) view.findViewById(R.id.pj_user_xx);
            pj_user_xx.setIsdown(false);
            pj_user_xx.setNum(5);
            pj_user_xx.setdownnum(Float.parseFloat(data.getRemark().get(0).getRemarkLevel()));
            NewPickEntity.DataBean.RemarkBean remark = a.get(0);
            TextView pj_user_phonenum = (TextView) view.findViewById(R.id.pj_user_phonenum);

            String userPhone = remark.getUserPhone();
            String substring = userPhone.substring(3, 7);
            String replace = userPhone.replace(substring, "****");
            pj_user_phonenum.setText(replace);

            TextView bcd = (TextView) view.findViewById(R.id.bcd);
            String remarkPen = remark.getRemarkPen();
            bcd.setText(remarkPen);
            TextView pj_user_num = (TextView) view.findViewById(R.id.pj_user_num);
            int remarkNum = remark.getRemarkNum();
            pj_user_num.setText(remarkNum + "");
            CircleImageView pj_user_photo = (CircleImageView) view.findViewById(R.id.pj_user_photo);
            Picasso.with(getContext()).load(remark.getUserImg()).placeholder(R.mipmap.logo).into(pj_user_photo);

            String remarkValue = remark.getRemarkValue();
            pj_user_value.setText(remarkValue);
        } else {
            view.findViewById(R.id.pj_1).setVisibility(View.GONE);
            view.findViewById(R.id.pj_2).setVisibility(View.GONE);
            pj_user_value.setText("暂无评论");
        }
        view.findViewById(R.id.pj_user_all).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DetailsActivity activity = (DetailsActivity) getActivity();
                activity.setvpseleter(1);
                activity.setAc_detalis_vp(1);

            }
        });
        /*
        ===================================================华丽丽的分割线=======================================================
         */
        //collStatus 1加了0沒有

    }

    private void overmoney() {
//        pick_whyuanjia
//        pick_whchild
//        pick_whtem
        getnum = pick_whyuanjia.getnum();
        aFloat = Float.valueOf(top.getData().getAdult());
        getnum1 = pick_whchild.getnum();
        aFloat1 = Float.valueOf(top.getData().getChild());
        getnum2 = pick_whtem.getnum();
        aFloat2 = Float.valueOf(top.getData().getTeam());

        pick_zongjia.setText("总价：¥" + ((getnum * aFloat) + (getnum1 * aFloat1) + (getnum2 * aFloat2)));


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fragment_detalis_top_btn_add:
            case R.id.fragment_detatop_lin_gw:

                break;
        }
    }

    @Override
    public void onwh(View view, int num, int type) {
        WhView v = (WhView) view;
        if (type == 0) {
            //增
            v.setNum(num + 1);
        } else {
            //减
            v.setNum(num - 1);
        }
        overmoney();

    }

    class MyvpAdapter extends PagerAdapter {
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = views.get(position);
            container.addView(view);
            return view;

        }

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object o) {
            return view == o;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//            super.destroyItem(container, position, object);
            container.removeView(views.get(position));
        }
    }
}
